package com.lagou.page.controller;

import com.lagou.common.pojo.Products;
import com.lagou.page.feign.ProductFeign;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
@RequestMapping("/page")
public class PageController {

//    @Autowired
//    private RestTemplate restTemplate;


    //@Autowired
    //private DiscoveryClient discoveryClient;

    @Autowired
    private ProductFeign productFeign;

    @GetMapping("/getData/{id}")
    public Products findDataById(@PathVariable Integer id){
        return productFeign.query(id);
    }

    @RequestMapping("/getPort")
    public String getProductServerPort(){
        return productFeign.findServerPort();
    }


    /**
     * 熔断服务测试  提供者模拟处理超时，调用方法添加Hystrix控制
     * @return
     */
    @HystrixCommand(
            // 线程池标识，要保持唯一，不唯一的话就共用了
            threadPoolKey = "getProductServerPort2",
            // 线程池细节属性配置
            threadPoolProperties = {
                    @HystrixProperty(name = "coreSize", value = "1"), // 线程数
                    @HystrixProperty(name = "maxQueueSize", value = "20") // 等待队列长度
            },

            // commandProperties熔断的一些细节属性配置
            commandProperties = {
                // 每一个属性都是一个HystrixProperty
                @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="2000")
            }

    )
    @RequestMapping("/getPort2")
    public String getProductServerPort2(){
        return productFeign.findServerPort();
    }

    @HystrixCommand(
            commandProperties = {
                    // 每一个属性都是一个HystrixProperty
                    @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="2000"),

                    @HystrixProperty(name ="metrics.rollingStats.timeInMilliseconds",value = "8000"),

                    //统计窗口内的最小请求数
                    @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "2"),

                    //统计窗口内错误请求阈值的设置 50%
                    @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "50"),

                    //自我修复的活动窗口时间
                    @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "3000")

            },
            fallbackMethod = "myFallBack" // 回退方法
    )
    @RequestMapping("/getPort3")
    public String getProductServerPort3(){
        return productFeign.findServerPort();

    }
    //服务降级方法  返回兜底数据
    public String myFallBack(){
        return "-1"; // 兜底数据
    }

}
